package edu.gznu.ics.fast.pulsar.service.prestoacc.taskService.ComputeTask.impl;

import edu.gznu.ics.fast.pulsar.dao.prestoacc.task.CacheServerSignal;
import edu.gznu.ics.fast.pulsar.dao.prestoacc.task.TaskIdMapper;
import edu.gznu.ics.fast.pulsar.dao.prestoacc.task.TaskMapper;
import edu.gznu.ics.fast.pulsar.dao.prestoacc.task.TaskParameterMapper;
import edu.gznu.ics.fast.pulsar.model.po.prestoacc.task.computeTask.TaskIdPO;
import edu.gznu.ics.fast.pulsar.model.po.prestoacc.task.computeTask.parameter.TaskParameterPO;
import edu.gznu.ics.fast.pulsar.model.vo.prestoacc.task.computeTask.TaskListVO;
import edu.gznu.ics.fast.pulsar.model.vo.user.TdUserVO;
import edu.gznu.ics.fast.pulsar.service.prestoacc.taskService.ComputeTask.ComputeTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by fei-zyx on 2016/10/11.
 */
@Service("ComputeTaskService")

public class ComputeTaskServiceImply implements ComputeTaskService {
    @Autowired
    TaskIdMapper taskIdMapper;
    @Autowired
    TaskParameterMapper taskParameterMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    CacheServerSignal cacheServerSignal;

    @Override
    public void addNewTaskId() {
        taskIdMapper.queryTaskByTaskId();
        //taskMapper.quer
        if (true) {
            taskIdMapper.queryMaxComputeTaskId();
            taskIdMapper.insertNewComputeTaskId(new TaskIdPO());
        }
        //////////////////


      /*  task
        taskMapper.insertNewTaskByTaskId();*/
        //taskIdMapper.insertNewTaskIdByUser();
        taskParameterMapper.insertComputeParameter(new TaskParameterPO());//////////////////////
        //insertNewTaskPythonParaByTaskId();

    }

    @Override
    public void addNewTaskList() {

    }

    @Override
    public List<TaskListVO> findTaskListByUser() {

        return taskMapper.queryTaskListByUser();
    }


    @Override
    public boolean checkComputeStatus() {
        taskMapper.checkCalover();
        return true;
    }

    @Override
    public void setControlFieldsValid(int i) {
        taskIdMapper.updateActivityTaskValidZero();
        //taskIdMapper.updateTaskIdValid();
        taskMapper.updateCalOverZero();

    }

    @Override
    public boolean startComputeTask() {
        int cacheServerSignalFlag = cacheServerSignal.queryCacheServerSignalFlag();
        cacheServerSignal.updateCacheServerSignalValid();
        Timer timer = new Timer();//实例化Timer类
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("退出");
                this.cancel();
            }
        }, 3000);
        if (cacheServerSignal.queryCacheServerSignalFlag() - 1 == cacheServerSignalFlag)
            return true;
        else

            return false;
    }

    @Override
    public List<TaskIdPO> findAllTaskIdByUser(TdUserVO tdUserVO) {

        return null;
        //return  taskIdMapper.queryAllTaskIdByUser(tdUserVO);
    }

    @Override
    public List<TaskIdPO> findAllTaskId() {
        return taskIdMapper.queryAllTaskId();
    }





/*

    @Autowired
    private TaskIdMapper addTaskIdMapper;
    public TaskIdPO addComputeTaskId(){
        TaskIdPO taskId=new TaskIdPO();

        int MaxComputeId = addTaskIdMapper.findMaxComputeTaskId();
        taskId.setTaskId(MaxComputeId);
        //taskId=addTaskIdMapper.addnewComputeTaskId(taskId);
        return  taskId;
    }

    @Override
    public void addNewTaskId() {

    }

    @Override
    public void addNewTaskList() {

    }
*/

   /* private int addnewComputeTaskId() {


        int taskId=0;
        return taskId;
    }

    private int findMaxComputeTaskId() {

        int MaxComputeId=0;
        return MaxComputeId;
    }*/
}
